package com.hzjq.core.worker

import com.hzjq.core.BlastDelegate
import com.hzjq.core.bean.CapProgressEntity
import com.hzjq.core.bean.ChargeProgressEntity
import com.hzjq.core.callback.ProgressCallback
import com.hzjq.core.massage.DataMessageBean
import com.hzjq.core.receive.Receiver
import com.hzjq.core.receive.Receives
import com.hzjq.core.work.Work

class ElectricVoltageWork : Work<CapProgressEntity> {


    constructor(callback: ProgressCallback<CapProgressEntity>?) : super(callback){
        Receives.getInstance().onInterceptorClearReceiver()
    }

    override fun cancel() {
        isStop = true
        Receives.getInstance()
            .onInterceptorRemoveReceiver(
                BlastDelegate.getDelegate().getAssemblyCmdLoader().getChargeEndQueryCmd()
            )
    }


    override fun doWork(vararg args: Any) {
        val cpe = ChargeProgressEntity()
        Receives.getInstance()
            .registerReceiver(
                BlastDelegate.getDelegate().getAssemblyCmdLoader().getChargeEndQueryCmd(),
                object : Receiver {
                    override fun convert(msg: String): Any {
                        return BlastDelegate.getDelegate().getParseLoader()
                            .parseScanProgress(msg)
                    }

                    override fun onSuccess(msg: Any) {
                        if (msg is CapProgressEntity) {
                            cpe.isEnd = false
                            cpe.mElectric = msg.mElectric
                            cpe.mVoltage = msg.mVoltage
                            cpe.progress = msg.progress
                            cpe.stateCode = msg.stateCode
                            callback?.onResult(cpe)
                            if(!isStop) {
                                doWork()
                            }
                        }

                    }

                    override fun failed() {
                    }

                })
        val msg = DataMessageBean(
            BlastDelegate.getDelegate().getAssemblyCmdLoader().getChargeEndQueryCmd().cmd
        )
        BlastDelegate.getDelegate().getCmdExeLoader().exeDelayPollResultCmd(msg.assembly(), BlastDelegate.getDelegate().getQueryDelayTime(),callback)

    }


    private var isStop = false

    fun isStop(isStop:Boolean){
        this.isStop = isStop
    }
}